package com.zam.config;

import com.zam.Interceptor.PRInterceptor;
import com.zam.Interceptor.TokenIterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author talent_dog
 * @Date 2023/12/15 20:00
 * @Version 1.0
 */

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //指定自定义的拦截器
        registry.addInterceptor(TokenIterceptor())
                .addPathPatterns("/**") //拦截所有请求，通过判断token是否合法来决定是否需要登录
                .excludePathPatterns("/user/login", "/user/register","/file/download/**","/capture/**"); //排除不需要拦截的请求
        //放重复提交的拦截器
        registry.addInterceptor(PRInterceptor())
                .addPathPatterns("/**") //拦截所有请求，通过判断token是否合法来决定是否需要登录
                .excludePathPatterns("/capture/**");
    }

    //将这个拦截器注册到容器中
    @Bean
    public TokenIterceptor TokenIterceptor() {
        return new TokenIterceptor(stringRedisTemplate);
    }
    //将这个拦截器注册到容器中
    @Bean
    public PRInterceptor PRInterceptor() {
        return new PRInterceptor(stringRedisTemplate);
    }
}